import { invokeAsyncAPi, invokeAPi } from '@/xz-fn.js'
import { getSecurePub } from '@/api/login'
import { isPassVersionByApp, isPassVersion } from '@/utils/judge'
import { pkgByCid } from '@/utils/costum'
const state = {
  info: {
    needProtocol: false,
    merchant: '', // 商户id
    semShortId: '', // sem上报短id
    appID: '', // 客户端id
    app_name: '', // 应用名称
    installApp: [],
    channel: '', // 渠道信息
    uuid: '', // 设备信息
    device_name: '', // 设备名称
    version: '', // 版本号
    pubkey: '',
    allPackages: [], // 在售权益包
    isLXAnonymousLogin: false // 是否支持联想匿名登录
  },
  appCfg: {
    name: '小智桌面',
    logo: 'pc',
    site: 'https://xzdesktop.cqttech.com'
  }
}

const mutations = {
  SET_DEVICEINFO (state, deviceInfo) {
    let allPackages = []
    const appNameObj = {
      22: {
        name: '小智桌面',
        logo: 'pc',
        site: 'https://xzdesktop.cqttech.com'
      },
      108: {
        name: '小智搜搜',
        logo: 'soso-icon',
        site: 'https://xzsearch.cqttech.com'
      },
      111: {
        name: '小智ToDo',
        logo: 'todo-icon',
        site: 'https://xztodo.cqttech.com'
      },
      125: {
        name: '小智壁纸',
        logo: 'xzpaper-icon',
        site: 'https://xzpaper.cqttech.com'
      },
      124: {
        name: 'Clean Desktop',
        logo: 'cleandesktop-icon',
        site: 'https://cleandesktop.kangaroooffice.com'
      },
      144: {
        name: 'Every文件搜索',
        logo: 'icon-everything',
        site: 'https://everything.cqttech.com'
      }
    }
    // deviceInfo.installApp = '10001,10003'
    // deviceInfo.version = '3.3.7.8'
    // deviceInfo.appID = '22'
    state.info.appID = deviceInfo.appID
    state.info.app_name = deviceInfo.app_name
    state.info.installApp = deviceInfo.installApp ? deviceInfo.installApp.split(',') : ['10001', '10003', '10004']
    state.info.channel = deviceInfo.unionid
    state.info.uuid = deviceInfo.pcid
    state.info.device_name = deviceInfo.device_name
    state.info.device_name = deviceInfo.device_name
    state.info.version = deviceInfo.version || '3.0.0.32'
    for (const item of state.info.installApp) {
      allPackages = allPackages.concat(pkgByCid[item])
    }
    if (deviceInfo.appID === '22' && isPassVersion('3.3.0.0')) {
      allPackages = allPackages.filter(item => item !== '40000')
    }
    // if (deviceInfo.appID === '22' && isPassVersion('3.0.2')) {
    //   allPackages.push('90000')
    // }
    state.info.allPackages = allPackages
    state.info.isLXAnonymousLogin = state.info.merchant === 'lenovo' &&
      isPassVersionByApp({
        '22': { v: '3.2.1.14' },
        '144': { v: '1.0.0.0' },
        '108': { v: '4.0.7.0' },
        '111': { v: '3.2.7.10' }
      })
    if (appNameObj[deviceInfo.appID]) {
      state.appCfg = appNameObj[deviceInfo.appID]
    }
    if (deviceInfo.app_name === 'XZDesktopCalendar') {
      state.appCfg.name = '小智桌面日历'
      state.appCfg.logo = 'logo-rili'
    }
    if (['224019', '1084209', '1105010', '1114409', '1254608'].includes(deviceInfo.appID + deviceInfo.unionid)) {
      state.info.needProtocol = true
    }
  },
  SET_PUBKEY: (state, count) => {
    state.pubkey = count
  },
  SET_MERCHANT: (state, id) => {
    const obj = {
      'b8083e34d7a710': 'lenovo',
      '3452690467': '360'
    }
    state.info.merchant = obj[id] || 'xz'
  },
  SET_SEMSHORTID: (state, id) => {
    state.info.semShortId = id
  }
}

const actions = {
  initDeviceInfo ({ commit, dispatch }) {
    return Promise.allSettled([dispatch('getDeviceInfo'), getSecurePub()]).then(res => {
      const res1 = res[1].value
      const pubkey = res1.data
      commit('SET_PUBKEY', pubkey)
    }).catch(() => {})
    // invokeAsyncAPi('xz_get_device_info').then(res => {
    //   commit('SET_DEVICEINFO', res)
    //   resolve()
    // }).catch(e => {
    //   reject(e)
    // })
  },
  getDeviceInfo ({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      invokeAsyncAPi('xz_get_device_info').then(res => {
        const merchant = invokeAPi('xz_get_merchant_Id')
        const shortId = invokeAPi('xz_get_semshort_Id')
        commit('SET_SEMSHORTID', shortId)
        commit('SET_MERCHANT', merchant)
        commit('SET_DEVICEINFO', res)
      }).finally(() => {
        resolve()
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
